<?php

use App\Model\RulesGroupDetail;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class AdminsTableSeeder extends Seeder
{
    /**
     *
     * 生成root管理员
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        $faker = app(Faker\Generator::class);

        $password = $faker->password;

        $data = [
            'username' => 'root',
            'password' => $password,
            'email' => 'test@qq.com'
        ];

        $re = \App\Model\Admin::where('username', 'root')->first();

        $admin_id = 0;

        if ($re) {

            $admin_id = $re->id;

            $re->fillable($data)->fill($data)->save();

            echo '初始密码更新为  ' . $password . PHP_EOL;

        } else {

            $admin = new \App\Model\Admin();

            $admin->fillable($data)->fill($data)->save();

            $admin_id = $admin->id;

            echo '初始密码为  ' . $password . PHP_EOL;
        }

        //设置为超级管理员
        RulesGroupDetail::updateOrCreate(['uid' => $admin_id], ['uid' => $admin_id, 'rules_group_id' => 0]);


        if (!\App\Model\Category::find(1)) {

            DB::table('category')->insert([
                ['pid' => 0, 'lv' => 1, 'name' => '商品分类','id'=>1],
                ['pid' => 1, 'lv' => 2, 'name' => '厨房用具','id'=>2],
                ['pid' => 2, 'lv' => 3, 'name' => '菜刀','id'=>3],
            ]);


        }


    }
}
